package cn.codergege.demo.selenium

import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.support.ui.WebDriverWait
import org.springframework.boot.test.context.SpringBootTest
import org.openqa.selenium.support.ui.ExpectedConditions


//@SpringBootTest
class SeleniumDemoTest {
    lateinit var driver: WebDriver
    lateinit var wait: WebDriverWait
    lateinit var url: String

//    @BeforeEach
    fun setup() {
        val chromeOptions = ChromeOptions()
        chromeOptions.addArguments("headless")
        // driver = ChromeDriver()
        driver = ChromeDriver(chromeOptions)
        // chromeOptions.addArguments("no-sandbox") // 禁用沙盒
        wait = WebDriverWait(driver, 10)
        url = "https://www.baidu.com"
    }

//    @Test
    fun testGet() {
        driver.get(url)
        val input = driver.findElement(By.id("kw"))
        val btnSearch = driver.findElement(By.id("su"))
        input.sendKeys("kotlin")
        Thread.sleep(1000)
        btnSearch.click()
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("content_left")))
//        println(driver.pageSource)
        println(driver.currentUrl)
        println(driver.title)
    }

//    @AfterEach
    fun clean() {
        driver.close()
    }
}